﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;

namespace SlamDunk.CMS.Common
{
    public class FileAssistant
    {
        #region 00.静态当前上下文对象属性 Current
        /// <summary>
        /// 将构造函数私有化
        /// </summary>
        private FileAssistant() { }

        /// <summary>
        /// 当前FileAssistant实例，类似HttpContext.Current
        /// </summary>
        public static FileAssistant Current
        {
            get
            {
                //从线程缓存中取，如果没有new一个，然后再存入线程缓存
                FileAssistant fileAssitant = CallContext.GetData(typeof(FileAssistant).Name) as FileAssistant;
                if (fileAssitant == null)
                {
                    fileAssitant = new FileAssistant();
                    CallContext.SetData(typeof(FileAssistant).Name, fileAssitant);
                }
                return fileAssitant;
            }
        }
        #endregion

        #region 01.读取文件方法
        /// <summary>
        /// 读取文件方法
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>内容字符串</returns>
        public string ReadFile(string path)
        {

            StreamReader reader = null;
            try
            {
                reader = new StreamReader(path, Encoding.UTF8);
                string content = reader.ReadToEnd();
                return content;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                reader.Close();
                reader.Dispose();
            }
        }
        #endregion

        #region 02.写入文件方法
        /// <summary>
        /// 写入文件方法
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="content">内容</param>
        /// <returns>写入结果</returns>
        public bool WriteFile(string path, string content)
        {

            FileInfo file = new FileInfo(path);
            StreamWriter writer = null;
            if (file.Exists)
            {
                file.Delete();
            }
            try
            {
                //获取文件目录并判断是否存在
                string directory = Path.GetDirectoryName(path);
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                writer = new StreamWriter(path, false, Encoding.UTF8);
                writer.Write(content);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                    writer.Dispose();
                }
            }
        }
        #endregion

        #region 03.复制文件夹方法
        /// <summary>
        /// 复制文件夹方法
        /// 成功返回success，失败返回错误信息
        /// </summary>
        /// <param name="sourcePath">源路径</param>
        /// <param name="targetPath">目标路径</param>
        /// <returns></returns>
        public string CopyFolder(string sourcePath, string targetPath)
        {
            string flag = "success";
            try
            {
                // 创建目标文件夹
                if (!Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }

                // 拷贝文件
                DirectoryInfo sourceDir = new DirectoryInfo(sourcePath);
                FileInfo[] fileArray = sourceDir.GetFiles();
                foreach (FileInfo file in fileArray)
                {
                    file.CopyTo(string.Format("{0}\\{1}", targetPath, file.Name), true);
                }

                // 循环子文件夹，递归
                DirectoryInfo targetDir = new DirectoryInfo(targetPath);
                DirectoryInfo[] subDirArray = sourceDir.GetDirectories();
                foreach (DirectoryInfo subDir in subDirArray)
                {
                    CopyFolder(subDir.FullName, string.Format("{0}//{1}", targetPath, subDir.Name));
                }
            }
            catch (Exception ex)
            {
                flag = ex.ToString();
            }
            return flag;
        }
        #endregion
    }
}
